package com.shine.minepressure.ui.realTime;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.interfaces.SimpleCallback;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shine.minepressure.BR;
import com.shine.minepressure.R;
import com.shine.minepressure.base.BaseFragment;
import com.shine.minepressure.bean.AreaMessageBean;
import com.shine.minepressure.bean.AreaSensorMessageBean;
import com.shine.minepressure.bean.SensorMessageBean;
import com.shine.minepressure.bean.StationMessageBean;
import com.shine.minepressure.databinding.FragmentRealTime4Binding;
import com.shine.minepressure.http.ApiException;
import com.shine.minepressure.http.ECObserver;
import com.shine.minepressure.http.RetrofitClient;
import com.shine.minepressure.http.RxUtils;
import com.shine.minepressure.http.Service;
import com.shine.minepressure.ui.adapter.RealTimeFragment4Adapter;
import com.shine.minepressure.ui.realTime.viewModel.RealTimeDataChildViewModel;
import com.shine.minepressure.view.pop.RealTimePopup;

import java.util.ArrayList;
import java.util.List;

public class RealTimeDataChild4Fragment extends BaseFragment<FragmentRealTime4Binding, RealTimeDataChildViewModel> {
    private RealTimeFragment4Adapter realTimeFragment4Adapter;
    private RealTimePopup realTimePopup;

    private int mAreaPosition, mSensorTypePosition, mAreaSensorMessagePosition;
    private String mAreaName, mSensorType, mSensorTypeName;
    private String mAreaId, mSensorTypeId, mSensorTypeNameId;
    private static List<AreaMessageBean.RowsDTO> mAreaMessageList = new ArrayList<>();
    private static List<String> mAreaMessage = new ArrayList<>();
    private static List<SensorMessageBean.RowsDTO> mSensorMessageList = new ArrayList<>();
    private static List<String> mSensorMessage = new ArrayList<>();
    private List<AreaSensorMessageBean.RowsDTO> areaSensorMessageList;
    private List<String> areaSensorMessage = new ArrayList<>();

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_real_time4;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public RealTimeDataChildViewModel initViewModel() {
        return new RealTimeDataChildViewModel(getActivity());
    }

    @Override
    public void initData() {
        super.initData();

        realTimeFragment4Adapter = new RealTimeFragment4Adapter(binding.recyclerView);
        binding.recyclerView.setAdapter(realTimeFragment4Adapter);

        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                binding.refreshLayout.finishRefresh(1000);
                binding.refreshLayout.setEnableLoadMore(true);
                realTimeFragment4Adapter.clear();
//                page = 1;
                getStationMessage();
            }
        });
        binding.refreshLayout.setEnableLoadMore(false);
//        binding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                binding.refreshLayout.finishLoadMore(1000);
//                page++;
//                getSensorRtMessage();
//            }
//        });

        binding.search.setOnClickListener(this);
        getStationMessage();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search:
//                search();
                break;
        }
    }


    public static void setAreaMessage(List<AreaMessageBean.RowsDTO> areaMessageList, List<String> areaMessage) {
        mAreaMessageList.clear();
        mAreaMessage.clear();
        mAreaMessageList.addAll(areaMessageList);
        mAreaMessage.addAll(areaMessage);
    }

    public static void setSensorMessage(List<SensorMessageBean.RowsDTO> sensorMessageList, List<String> sensorMessage) {
        mSensorMessageList.clear();
        mSensorMessage.clear();
        mSensorMessageList.addAll(sensorMessageList);
        mSensorMessage.addAll(sensorMessage);
    }


    /**
     * 获取指定测区传感器
     */
    private void getAreaSensorMessage() {
        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .getAreaSensorMessage(mAreaId, mSensorTypeId)
                .compose(RxUtils.bindToLifecycle(this))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .subscribe(new ECObserver<AreaSensorMessageBean>() {
                    @Override
                    protected void _onNext(AreaSensorMessageBean areaSensorMessageBean) {
                        dismissLoading();

                        if (areaSensorMessageBean.getTotal().equals("0"))
                            return;

                        areaSensorMessageList = areaSensorMessageBean.getRows();
                        for (AreaSensorMessageBean.RowsDTO rowsDTO : areaSensorMessageList) {
                            areaSensorMessage.add(rowsDTO.getSensorTypeName());
                        }

                        realTimePopup.setAreaSensorMessage(areaSensorMessage);
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }

    /**
     * 分站信息
     */
    private void getStationMessage() {
        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .getStationMessage()
                .compose(RxUtils.bindToLifecycle(this))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .subscribe(new ECObserver<StationMessageBean>() {
                    @Override
                    protected void _onNext(StationMessageBean stationMessageBean) {
                        dismissLoading();
                        binding.totalNum.setText(stationMessageBean.getTotal() + "");
                        realTimeFragment4Adapter.addMoreData(stationMessageBean.getRows());
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }

    private void search() {
        realTimePopup = new RealTimePopup(getActivity(), mAreaMessage, mSensorMessage, mAreaName, mSensorType, mSensorTypeName,
                mAreaPosition, mSensorTypePosition, mAreaSensorMessagePosition);
        new XPopup.Builder(getActivity())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
//                        .isThreeDrag(true) //是否开启三阶拖拽，如果设置enableDrag(false)则无效
                .setPopupCallback(new SimpleCallback() {
                    @Override
                    public void onShow(BasePopupView popupView) {
                    }

                    @Override
                    public void onDismiss(BasePopupView popupView) {
                    }
                })
                .asCustom(realTimePopup)/*.enableDrag(false)*/
                .show();
        realTimePopup.show();
        realTimePopup.setListener(new OnConfirmListener() {
            @Override
            public void onConfirm() {
            }
        }, new OnCancelListener() {
            @Override
            public void onCancel() {
            }
        }, new RealTimePopup.OnPopClickListener() {

            @Override
            public void onPopClick(View view, Integer areaPosition, Integer sensorTypePosition) {
                if (view.getId() == R.id.areaSensorMessageLayout) {

                    mAreaId = mAreaMessageList.get(areaPosition).getArea_Id();
                    if (sensorTypePosition == null)
                        mSensorTypeId = "";
                    else
                        mSensorTypeId = mSensorMessageList.get(sensorTypePosition).getSensorTypeID();
                    getAreaSensorMessage();
                }
            }

            @Override
            public void onConfirmClick(Integer areaPosition, Integer sensorTypePosition, Integer areaSensorMessagePosition,
                                       String areaId, String sensorType, String sensorTypeName) {
                mAreaPosition = areaPosition;
                mSensorTypePosition = sensorTypePosition;
                mAreaSensorMessagePosition = areaSensorMessagePosition;
                mAreaName = areaId;
                mSensorType = sensorType;
                mSensorTypeName = sensorTypeName;

//                binding.refreshLayout.setEnableLoadMore(true);
//                realTimeFragment1Adapter.clear();
//                page = 1;
                getStationMessage();
            }
        });
    }

}